<template>
  <div class="member-list-container">
    <!-- 搜索和操作栏 -->
    <div class="search-bar">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="党员姓名">
          <el-input v-model="searchForm.name" placeholder="请输入党员姓名" clearable />
        </el-form-item>
        <el-form-item label="党员类型">
          <el-select v-model="searchForm.type" placeholder="请选择党员类型" clearable>
            <el-option label="正式党员" value="formal" />
            <el-option label="预备党员" value="probationary" />
            <el-option label="发展对象" value="development" />
            <el-option label="入党积极分子" value="activist" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属党支部">
          <el-select v-model="searchForm.branch" placeholder="请选择党支部" clearable>
            <el-option label="技术部党支部" value="tech" />
            <el-option label="行政部党支部" value="admin" />
            <el-option label="市场部党支部" value="market" />
            <el-option label="财务部党支部" value="finance" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>搜索
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>
      
      <!-- 操作按钮 -->
      <div class="action-buttons">
        <el-button type="primary" @click="handleAdd">
          <el-icon><Plus /></el-icon>添加党员
        </el-button>
        <el-button type="success" @click="handleExport">
          <el-icon><Download /></el-icon>导出数据
        </el-button>
        <el-button type="danger" @click="handleBatchDelete" :disabled="selectedMembers.length === 0">
          <el-icon><Delete /></el-icon>批量删除
        </el-button>
      </div>
    </div>
    
    <!-- 数据表格 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      stripe
      @selection-change="handleSelectionChange"
      v-loading="loading"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="70" />
      <el-table-column prop="name" label="姓名" min-width="120" />
      <el-table-column prop="gender" label="性别" width="80">
        <template #default="scope">
          {{ scope.row.gender === 'male' ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="80" />
      <el-table-column prop="phone" label="联系电话" min-width="140" />
      <el-table-column prop="type" label="党员类型" min-width="120">
        <template #default="scope">
          <el-tag
            :type="getMemberTypeTag(scope.row.type)"
          >
            {{ getMemberTypeName(scope.row.type) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="branch" label="所属党支部" min-width="140" />
      <el-table-column prop="joinDate" label="入党日期" min-width="120" sortable />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag
            :type="scope.row.status === 'active' ? 'success' : 'danger'"
          >
            {{ scope.row.status === 'active' ? '在职' : '离职' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="220">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">
            <el-icon><View /></el-icon>查看
          </el-button>
          <el-button type="success" link @click="handleEdit(scope.row)">
            <el-icon><Edit /></el-icon>编辑
          </el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">
            <el-icon><Delete /></el-icon>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.pageSize"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { 
  Search, Refresh, Plus, Download, Delete, View, Edit 
} from '@element-plus/icons-vue';

const router = useRouter();

// 搜索表单
const searchForm = reactive({
  name: '',
  type: '',
  branch: ''
});

// 表格数据
const tableData = ref([]);
const loading = ref(false);
const selectedMembers = ref([]);

// 分页
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 搜索
const handleSearch = () => {
  fetchData();
};

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = '';
  });
  fetchData();
};

// 获取数据
const fetchData = async () => {
  loading.value = true;
  try {
    // 这里应该调用API获取数据
    // 模拟API调用
    setTimeout(() => {
      // 生成模拟数据
      const data = [];
      for (let i = 1; i <= 30; i++) {
        data.push({
          id: i,
          name: `党员${i}`,
          gender: i % 3 === 0 ? 'female' : 'male',
          age: 25 + (i % 30),
          phone: `1381234${i.toString().padStart(4, '0')}`,
          type: ['formal', 'probationary', 'development', 'activist'][i % 4],
          branch: ['技术部党支部', '行政部党支部', '市场部党支部', '财务部党支部'][i % 4],
          joinDate: `2020-${(i % 12) + 1}-${(i % 28) + 1}`,
          status: i % 10 === 0 ? 'inactive' : 'active'
        });
      }
      
      tableData.value = data;
      pagination.total = 100;
      loading.value = false;
    }, 500);
  } catch (error) {
    console.error(error);
    loading.value = false;
  }
};

// 表格选择变化
const handleSelectionChange = (selection) => {
  selectedMembers.value = selection;
};

// 分页大小变化
const handleSizeChange = (size) => {
  pagination.pageSize = size;
  fetchData();
};

// 分页页码变化
const handleCurrentChange = (page) => {
  pagination.currentPage = page;
  fetchData();
};

// 添加党员
const handleAdd = () => {
  router.push('/party-members/add');
};

// 导出数据
const handleExport = () => {
  ElMessage.success('导出党员数据成功');
};

// 批量删除
const handleBatchDelete = () => {
  if (selectedMembers.value.length === 0) {
    return;
  }
  
  ElMessageBox.confirm(
    `确定要删除选中的${selectedMembers.value.length}名党员信息吗？此操作不可恢复！`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 执行删除操作
    ElMessage.success('批量删除成功');
    fetchData();
  }).catch(() => {});
};

// 查看党员详情
const handleView = (row) => {
  console.log('查看党员:', row);
  // 可以跳转到详情页或打开详情对话框
};

// 编辑党员
const handleEdit = (row) => {
  router.push(`/party-members/edit/${row.id}`);
};

// 删除单个党员
const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除"${row.name}"的信息吗？此操作不可恢复！`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 执行删除操作
    ElMessage.success(`删除"${row.name}"成功`);
    fetchData();
  }).catch(() => {});
};

// 党员类型标签颜色
const getMemberTypeTag = (type) => {
  const types = {
    formal: 'success',
    probationary: 'warning',
    development: 'info',
    activist: ''
  };
  return types[type] || '';
};

// 党员类型名称
const getMemberTypeName = (type) => {
  const types = {
    formal: '正式党员',
    probationary: '预备党员',
    development: '发展对象',
    activist: '入党积极分子'
  };
  return types[type] || type;
};

// 页面加载时获取数据
onMounted(() => {
  fetchData();
});
</script>

<style scoped>
.member-list-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.search-bar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
}

.action-buttons {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

@media (max-width: 768px) {
  .search-bar {
    flex-direction: column;
  }
  
  .action-buttons {
    margin-top: 15px;
    justify-content: flex-start;
  }
}
</style> 